﻿<%@ WebHandler Language="C#" Class="ReportTongJiOneFromHandler" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Collections;
public class ReportTongJiOneFromHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {

        string keywords = "";
        keywords = context.Request.QueryString["keywords"];
        KVModel model;


        string str = System.Configuration.ConfigurationManager.ConnectionStrings["decheatConnectionString"].ToString();

 //       string sql = "select t.couttimes,idc.name as provincename from " +
 //"( select r.Province_Code,r.R_SecondTypeId,COUNT(r.id) as couttimes from dbo.Dyh_baseDB db " +
 //"inner join Dyh_report r on db.id=r.seqID  " +
 //"where db.object='" + keywords + "' " +
 //"group by r.Province_Code,r.R_SecondTypeId) as t " +
 //"inner join Dyh_ID_code idc on t.Province_Code=idc.code ";
 //       string sql = "exec ReportTongJiOneFrom " + keywords;
        //"inner join Dyh_report_type rt on t.R_SecondTypeId=rt.id";
        
        string sql = "exec ReportTongJiOneFrom " + keywords;
 //"inner join Dyh_report_type rt on t.R_SecondTypeId=rt.id";
        Hashtable hash = new Hashtable();
        SqlConnection conn = new SqlConnection(str);
        conn.Open();
        SqlCommand comm = new SqlCommand(sql, conn);
        System.Data.DataSet ds = new DataSet();
        SqlDataReader dr = comm.ExecuteReader();
        while (dr.Read())
        {
            //string typename = dr["type"].ToString();
            string provincename = dr["provincename"].ToString();
            int num = int.Parse(dr["couttimes"].ToString());
            if (!hash.ContainsKey(provincename))
            {
                hash[provincename] = num;
            }
            else
            {
                hash[provincename] = int.Parse(hash[provincename].ToString()) + num;
            }
        }
        //JsonHelper.GetJson<List<Tongji>

        dr.Close();

        List<KVModel> resultList = new List<KVModel>();
        foreach (string key in hash.Keys)
        {
            int num = int.Parse(hash[key].ToString());
            model = new KVModel((key.Split(new string[]{ "省"},StringSplitOptions.None))[0], num);
            resultList.Add(model);
        }
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
        }
        //JavaScriptSerializer json = new JavaScriptSerializer();
        string result = WebUI.CommonClass.JSONHelper.Obj2Json<List<KVModel>>(resultList);
        context.Response.ContentType = "text/plain";


        context.Response.Write(result);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }


    public int getTotal()
    {
        string msg1 = "湖北省 黄石市";
        int start = msg1.IndexOf('省', 0);
        string sheng = msg1.Split(new string[] { "省", "自治区" }, StringSplitOptions.None)[0];

        Console.WriteLine(sheng);
        return start;
    }

}